package online.westonline.com.westonline;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;

import com.easy.west.westclient.client.WastClient;
import com.jaeger.library.StatusBarUtil;
import com.orhanobut.hawk.Hawk;
import com.tangxiaolv.telegramgallery.GalleryActivity;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.im.android.api.JMessageClient;
import online.westonline.com.westonline.ui.ConstactsActivity;
import online.westonline.com.westonline.ui.DepInfoActivity;
import online.westonline.com.westonline.ui.HomeActivity;
import online.westonline.com.westonline.ui.LoginActivity;
import online.westonline.com.westonline.ui.MeetFormActivity;
import online.westonline.com.westonline.ui.MeetRoomStateActivity;
import online.westonline.com.westonline.ui.MeetingActivity;
import online.westonline.com.westonline.ui.MeetingGaideActivity;
import online.westonline.com.westonline.ui.OneContactActivity;
import online.westonline.com.westonline.ui.OrederMeetActivity;
import online.westonline.com.westonline.ui.WorkPlanActivity;
import online.westonline.com.westonline.ui.WorkPlanEditActivity;
import online.westonline.com.westonline.ui.WorkPlanInfoActivity;
import online.westonline.com.westonline.ui.WorkPlanListActivity;
import online.westonline.com.westonline.utils.UiUtlis;


/**
 * 域
 * Created by admin on 2018/5/2.
 */

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks
{
    //聊天所需静态变量
    //暂未导包
    /**
     *
    public static final String CONV_TITLE = "conv_title";
    public static final int IMAGE_MESSAGE = 1;
    public static final int TAKE_PHOTO_MESSAGE = 2;
    public static final int TAKE_LOCATION = 3;
    public static final int FILE_MESSAGE = 4;
    public static final int TACK_VIDEO = 5;
    public static final int TACK_VOICE = 6;
    public static final int BUSINESS_CARD = 7;
    public static final int REQUEST_CODE_SEND_FILE = 26;


    public static final int RESULT_CODE_ALL_MEMBER = 22;
    public static Map<Long, Boolean> isAtMe = new HashMap<>();
    public static Map<Long, Boolean> isAtall = new HashMap<>();
    public static List<Message> forwardMsg = new ArrayList<>();

    public static long registerOrLogin = 1;
    public static final int REQUEST_CODE_TAKE_PHOTO = 4;
    public static final int REQUEST_CODE_SELECT_PICTURE = 6;
    public static final int REQUEST_CODE_CROP_PICTURE = 18;
    public static final int REQUEST_CODE_CHAT_DETAIL = 14;
    public static final int RESULT_CODE_FRIEND_INFO = 17;
    public static final int REQUEST_CODE_ALL_MEMBER = 21;
    public static final int RESULT_CODE_EDIT_NOTENAME = 29;
    public static final String NOTENAME = "notename";
    public static final int REQUEST_CODE_AT_MEMBER = 30;
    public static final int RESULT_CODE_AT_MEMBER = 31;
    public static final int RESULT_CODE_AT_ALL = 32;
    public static final int SEARCH_AT_MEMBER_CODE = 33;

    public static final int RESULT_BUTTON = 2;
    public static final int START_YEAR = 1900;
    public static final int END_YEAR = 2050;
    public static final int RESULT_CODE_SELECT_FRIEND = 23;

    public static final int REQUEST_CODE_SELECT_ALBUM = 10;
    public static final int RESULT_CODE_SELECT_ALBUM = 11;
    public static final int RESULT_CODE_SELECT_PICTURE = 8;
    public static final int REQUEST_CODE_BROWSER_PICTURE = 12;
    public static final int RESULT_CODE_BROWSER_PICTURE = 13;
    public static final int RESULT_CODE_SEND_LOCATION = 25;
    public static final int RESULT_CODE_SEND_FILE = 27;
    public static final int REQUEST_CODE_SEND_LOCATION = 24;
    public static final int REQUEST_CODE_FRIEND_INFO = 16;
    public static final int RESULT_CODE_CHAT_DETAIL = 15;
    public static final int ON_GROUP_EVENT = 3004;
    public static final String DELETE_MODE = "deleteMode";
    public static final int RESULT_CODE_ME_INFO = 20;

    public static final String DRAFT = "draft";
    public static final String GROUP_ID = "groupId";
    public static final String POSITION = "position";
    public static final String MsgIDs = "msgIDs";
    public static final String NAME = "name";
    public static final String ATALL = "atall";
    public static final String SEARCH_AT_MEMBER_NAME = "search_at_member_name";
    public static final String SEARCH_AT_MEMBER_USERNAME = "search_at_member_username";
    public static final String SEARCH_AT_APPKEY = "search_at_appkey";

    public static final String MEMBERS_COUNT = "membersCount";

    public static String PICTURE_DIR = "sdcard/GrandBroad/pictures/";
    private static final String JCHAT_CONFIGS = "JChat_configs";
    public static String FILE_DIR = "sdcard/GrandBroad/recvFiles/";
    public static String VIDEO_DIR = "sdcarVIDEOd/GrandBroad/sendFiles/";
    public static final String TARGET_ID = "targetId";
    public static final String ATUSER = "atuser";
    public static final String TARGET_APP_KEY = "targetAppKey";
    public static int maxImgCount;               //允许选择图片最大数
    public static final String GROUP_NAME = "groupName";

    //定位服务
    //public static LocationService locationService;
    //自己所属组列表
    public static List<GroupInfo> mGroupInfoList = new ArrayList<>();
    //好友列表
    public static List<UserInfo> mFriendInfoList = new ArrayList<>();
    //搜索所得的组列表
    public static List<UserInfo> mSearchGroup = new ArrayList<>();
    //搜索所得的人列表
    public static List<UserInfo> mSearchAtMember = new ArrayList<>();
    //消息列表
    public static List<Message> ids = new ArrayList<>();
    //已读消息
    public static List<UserInfo> alreadyRead = new ArrayList<>();
    //未读消息
    //都是item所需,需要在跳转前设置
    public static List<UserInfo> unRead = new ArrayList<>();
    public static List<String> forAddFriend = new ArrayList<>();




    private static Context context;
    private static List<Activity> activityList=new ArrayList<>();
    //测试
    private int activity_number=0;
     */
    /**
     * 屏幕宽度
     */
    public static int screenWidth;
    private static Context context;
    /**
     * 屏幕高度
     */
    public static int screenHeight;
    /**
     * 屏幕密度
     */
    public static float screenDensity;
    private static List<Activity> activityList=new ArrayList<>();
    private static WastClient client;
    @Override
    public void onCreate() {
        super.onCreate();
        Hawk.init(this).build();
        //注册极光im
        JMessageClient.init(getApplicationContext());
        //极光推送注册
        JPushInterface.init(getApplicationContext());
        JPushInterface.resumePush(this);
        //xutils
        x.Ext.init(this);
        registerActivityLifecycleCallbacks(this);
        initScreenSize();
        client = WastClient.getInstance(this);
        context = getApplicationContext();
        initservice();
    }

    public static WastClient getClient() {
        return client;
    }

    /**
     * 初始化服务
     */
    private void initservice() {

    }

    /**
     * 初始化当前设备屏幕宽高
     */
    private void initScreenSize() {
        DisplayMetrics curMetrics = getApplicationContext().getResources().getDisplayMetrics();
        screenWidth = curMetrics.widthPixels;
        screenHeight = curMetrics.heightPixels;
        screenDensity = curMetrics.density;
    }

    /***
     * 全局上下文 不可用于对话框
     * @return
     */
    public static Context getContext() {
        return context;
    }

    /**
     * 添加activity
     * @param activity
     */
    private void addActivity(Activity activity)
    {
        activityList.add(activity);
    }
    /**
     * 移除activity
     *
     */
    public static void removeactivity(Activity activity)
    {
        if (activityList.size()>0)
        {
            activityList.remove(activity);
            activity.finish();
        }
    }

    /**
     * 结束 退出
     */
    public static void exit()
    {
        if (activityList.size()>0)
        {
            for (Activity activity:activityList)
            {
                activity.finish();
            }
        }
        //杀掉有关app的所有进程
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * 注册
     */
    private void registEvent(Activity activity)
    {
        if (!EventBus.getDefault().isRegistered(activity))
        {
            EventBus.getDefault().register(activity);
        }
    }



    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        addActivity(activity);
        //其他未定制页面
        StatusBarUtil.setColor(activity,UiUtlis.getColor(R.color.colorAppTheme),125);
        //相册页面
        if (activity instanceof GalleryActivity)
        {
            //修改状态栏颜色
            StatusBarUtil.setColor(activity, UiUtlis.getColor(R.color.GrallyColor));
        }



        /*
        //相册页面
        if (activity instanceof GalleryActivity)
        {
            //修改状态栏颜色
            StatusBarUtil.setColor(activity, UiUtlis.getColor(R.color.GrallyColor));
        }
        //通讯录入口页面
        if (activity instanceof ContactsActivity)
        {
            StatusBarUtil.setColor(activity,UiUtlis.getColor(R.color.colorAppTheme),125);
        }
        //通讯录页面
        if (activity instanceof lConstactsBookActivity)
        {
            StatusBarUtil.setColor(activity,UiUtlis.getColor(R.color.colorAppTheme),125);
        }
        /*
        //私聊界面
        if (activity instanceof ChatActivity)
        {
            StatusBarUtil.setColor(activity,UiUtlis.getColor(R.color.colorAppTheme),125);
        }
        */
        //工作计划
        /*
        if (activity instanceof WorkPlanActivity) {
            StatusBarUtil.setColor(activity, UiUtlis.getColor(R.color.colorAppTheme), 125);
        }
        //聊天界面
        if (activity instanceof ChatActivity)
        {
            StatusBarUtil.setColor(activity, UiUtlis.getColor(R.color.colorAppTheme), 125);
        }
        */

    }

    @Override
    public void onActivityStarted(Activity activity) {
        //注册eventbus
        //1.仅为需要的访问的activity注册事件
        //2.不需要注册可以不用管
        //3.以下为所有需要请求后台数据的页面
        if (activity instanceof LoginActivity)
        {
            //登录
            registEvent(activity);
        }
        if (activity instanceof HomeActivity)
        {
            //主界面初始化相关
            registEvent(activity);
        }
        if (activity instanceof WorkPlanListActivity)
        {
            //日程列表
            registEvent(activity);
        }
        if (activity instanceof WorkPlanActivity)
        {
            //日程表
            registEvent(activity);
        }
        if (activity instanceof WorkPlanInfoActivity)
        {
            //日程详情
            registEvent(activity);
        }
        if (activity instanceof WorkPlanEditActivity)
        {
            //日程编辑
            registEvent(activity);
        }
        if (activity instanceof MeetingActivity)
        {
            //会议室列表
            registEvent(activity);
        }
        if (activity instanceof OneContactActivity)
        {
            //联系人详情
            registEvent(activity);
        }
        if (activity instanceof MeetFormActivity)
        {
            //基础表单信息
            registEvent(activity);
        }
        if (activity instanceof OrederMeetActivity)
        {
            //预约会议室界面
            registEvent(activity);

        }
        if (activity instanceof ConstactsActivity)
        {
            //通讯录页面
            registEvent(activity);
        }
        if (activity instanceof MeetRoomStateActivity)
        {
            //任务详情页面
            registEvent(activity);
        }
        if (activity instanceof MeetingGaideActivity)
        {
            //会议模板
            registEvent(activity);
        }
        if (activity instanceof DepInfoActivity)
        {
            //部门详情
            registEvent(activity);
        }
        /*
        if (activity instanceof lConstactsBookActivity)
        {
            //云通讯录
            registEvent(activity);
        }

        /*
        if (activity instanceof ChatActivity)
        {
            //聊天
            registEvent(activity);
        }
        */
        /*
        if (activity instanceof WorkplanEditActivity)
        {
            //编辑日程
            registEvent(activity);
        }
        if (activity instanceof WorkPlanActivity)
        {
            //日程界面
            registEvent(activity);
        }
        if (activity instanceof PlanInfoActivity)
        {
            //日程详情界面
            registEvent(activity);
        }
        if (activity instanceof WorkPlanListActivity)
        {
            //列表日程
            registEvent(activity);
        }
        */
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }
    @Override
    public void onActivityPaused(Activity activity) {

    }
    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    /***
     * 换到这里销毁时间
     * 及在acitvity没有销毁时一直接收事件
     * @param activity
     */
    @Override
    public void onActivityDestroyed(Activity activity) {
        //销毁事件
        if (EventBus.getDefault().isRegistered(activity))
        {
            EventBus.getDefault().unregister(activity);
        }
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        //卡顿时
        //1.基本不会发生
        //2.以防万一
        if (activityList!=null)
        {
            activityList.remove(0);
        }
    }

    /**
     * 保留某个页面,去除其他页面
     * @param loginActivityClass
     */
    public static void exitExcpetActivity(String loginActivityClass) {
       if (activityList.size()>0)
       {
           for (Activity activity:activityList)
           {
               if (!activity.getClass().getSimpleName().equals(loginActivityClass))
               {
                   activity.finish();
               }
           }
       }
    }
}
